package com.link.play.fit.ai.ui.weightcontrol.ac

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.addCallback
import androidx.lifecycle.ViewModelProvider
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.goToAc
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AcSetMealBinding
import com.link.play.fit.ai.kv.AuthCache.firstMeals
import com.link.play.fit.ai.kv.AuthCache.secondMeals
import com.link.play.fit.ai.kv.AuthCache.thirdMeals
import com.link.play.fit.ai.ui.dialog.SetTimeDialog

class SetMealAc : BaseActivity<AcSetMealBinding>(AcSetMealBinding::inflate) {

    val viewModel by lazy(LazyThreadSafetyMode.NONE) {
        ViewModelProvider(this)[WeighControlViewModel::class.java]
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel.lastUserGuide = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            intent.getSerializableExtra("data", UserGuide::class.java) ?: UserGuide()
        } else {
            @Suppress("DEPRECATION")
            (intent.getSerializableExtra("data") as UserGuide?) ?: UserGuide()
        }

        changeMealsUI()


        arrayOf(binding.ivAdd1, binding.ivEdit1).forEach { _view ->
            _view.setOnClickListener {
                SetTimeDialog(this).showDialog(if (firstMeals.isEmpty()) "08:10:AM" else firstMeals) {
                    firstMeals = it
                    viewModel.lastUserGuide.mealsTime = listOf(firstMeals, secondMeals, thirdMeals)
                    changeMealsUI()
                }
            }

            arrayOf(binding.ivAdd2, binding.ivEdit2).forEach { _view ->
                _view.setOnClickListener {
                    SetTimeDialog(this).showDialog(if (secondMeals.isEmpty()) "11:45:AM" else secondMeals) {
                        secondMeals = it
//                    val data = MealsTimeData(value = it, timeStamp = System.currentTimeMillis().toString())
//                    CloudStore.addToList(
//                        DOC_MEALS_TIME_LIST_DATA,
//                        DOC_MEALS_TIME_LIST_DATA,
//                        data.timeStamp,
//                        data
//                    )
                        viewModel.lastUserGuide.mealsTime = listOf(firstMeals, secondMeals, thirdMeals)
                        changeMealsUI()
                    }
                }
            }

            arrayOf(binding.ivAdd3, binding.ivEdit3).forEach { _view ->
                _view.setOnClickListener {
                    SetTimeDialog(this).showDialog(if (thirdMeals.isEmpty()) "06:10:PM" else thirdMeals) {
                        thirdMeals = it
                        viewModel.lastUserGuide.mealsTime = listOf(firstMeals, secondMeals, thirdMeals)
                        changeMealsUI()
                    }
                }
            }

            onBackPressedDispatcher.addCallback {
                finish()
                overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right)
            }
            binding.flBack.setOnClickListener {
                onBackPressedDispatcher.onBackPressed()
            }

            binding.tvNext.setOnClickListener {
                goToAc(WeighControlReadyAc::class.java) {
                    putExtra("data", viewModel.lastUserGuide)
                }
                overridePendingTransition(R.anim.slide_in_from_right, R.anim.slide_out_to_left)
            }

        }

    }

    private fun changeMealsUI(mealsTime: List<String> = viewModel.lastUserGuide.mealsTime) {
        kotlin.runCatching {
            val mealsFirst = mealsTime[0]
            if (mealsFirst.isEmpty()) {
                binding.tvTime1.visibility = View.GONE
                binding.ivEdit1.visibility = View.GONE
                binding.ivAdd1.visibility = View.VISIBLE
            } else {
                binding.tvTime1.visibility = View.VISIBLE
                binding.ivEdit1.visibility = View.VISIBLE
                binding.ivAdd1.visibility = View.GONE

                val split = mealsFirst.split(":")
                val hour = split[0]
                val min = split[1]
                val apm = split[2]
                binding.tvTime1.text = "$hour:$min $apm"
            }

            val mealsSecond = mealsTime[1]
            if (mealsSecond.isEmpty()) {
                binding.tvTime2.visibility = View.GONE
                binding.ivEdit2.visibility = View.GONE
                binding.ivAdd2.visibility = View.VISIBLE
            } else {
                binding.tvTime2.visibility = View.VISIBLE
                binding.ivEdit2.visibility = View.VISIBLE
                binding.ivAdd2.visibility = View.GONE

                val split = mealsSecond.split(":")
                val hour = split[0]
                val min = split[1]
                val apm = split[2]
                binding.tvTime2.text = "$hour:$min $apm"
            }

            val mealsThird = mealsTime[2]
            if (mealsThird.isEmpty()) {
                binding.tvTime3.visibility = View.GONE
                binding.ivEdit3.visibility = View.GONE
                binding.ivAdd3.visibility = View.VISIBLE
            } else {
                binding.tvTime3.visibility = View.VISIBLE
                binding.ivEdit3.visibility = View.VISIBLE
                binding.ivAdd3.visibility = View.GONE

                val split = mealsThird.split(":")
                val hour = split[0]
                val min = split[1]
                val apm = split[2]
                binding.tvTime3.text = "$hour:$min $apm"
            }
        }
    }

}